/**
 * Created by zycool on 2016/8/17.
 * 场站阀室 - 管道在线数据 - 基础数据
 */


;(function ($, window, document, undefined) {

    // 场站阀室类型
    var $room_type = $('#JQ_value_room_type').select3();
    // 是否具有收发球筒
    var $has_ball = $('#JQ_has_ball').select3({
        search: false,
        data: [
            {id:'1',text:'是'},
            {id:'0',text:'否'},
            {id:'2',text:'未知'}
        ]
    });
    // 是否具有阴保电源
    var $has_cathodic = $('#JQ_has_cathodic').select3({
        search: false,
        data: [
            {id:'1',text:'是'},
            {id:'0',text:'否'},
            {id:'2',text:'未知'}
        ]
    });
    // 是否具有放空装置
    var $has_device = $('#JQ_has_device').select3({
        search: false,
        data: [
            {id:'1',text:'是'},
            {id:'0',text:'否'},
            {id:'2',text:'未知'}
        ]
    });
    // 导出
    var $export_base= $('#JQ_export_base');
    // 表格
    var $table_room = $('#JQ_table_room');
    // 列表切换按钮
    var $list_span = $('#JQ_toggle_list_span');
    // 地图切换按钮
    var $map_span = $('#JQ_toggle_map_span');

    // 填充场站阀室类型
    DyyBase.ajax(baseDataApi.url_valve_room_type(), {}, function (dataset) {
        var json = DyyBase.getSelect(dataset);
        json.shift();
        $room_type.add(json);
    });

    // 条件对象
    var filter = {
        getSearch: function (param) {
            var data = {};
            data.mode = true;
            // 分页参数
            if (param) {
                if (param.start == 0) {
                    data['jsonBean.pageNum'] = 1;
                } else {
                    data['jsonBean.pageNum'] = param.start / 10 + 1;
                }
                data['jsonBean.pageCount'] = param.length;
            }
            // 场站阀室类型
            data.type = function(){
                var v = $room_type.val();
                if(v){
                    return v;
                }
                return '';
            }();
            data.isPiggingDeviceind = function(){
                var v = $has_ball.val();
                if(v){
                    return v;
                }
                return '';
            }();
            data.isPowerind = function(){
                var v = $has_cathodic.val();
                if(v){
                    return v;
                }
                return '';
            }();
            data.isEmptyingDevice = function(){
                var v = $has_device.val();
                if(v){
                    return v;
                }
                return '';
            }();
            return data;
        },
        getExport: function(){
            return filter.getSearch();
        }
    };

    // 初始加载表格
    var DTROOM = $table_room.DataTable({
        'language': {
            'paginate': {
                'next': '下一页',
                'previous': '上一页'
            },
            'info': '本次检索共 _TOTAL_ 条，每页显示 10 条，共 _PAGES_ 页',
            'zeroRecords': DyyBase.table_empty(),
            'infoEmpty': ''
        },
        'autoWidth': false,
        'order': [],
        'serverSide': true,
        'pageLength': 10,
        'ordering': false,
        'processing': false,
        'searching': false,
        'lengthChange': false,
        'ajax': function (data, callback) {
            var param = filter.getSearch(data);
            DyyBase.ajax(baseDataApi.url_valve_room_search(), param, function (dataset) {
                var returnData = {};
                returnData.draw = data.draw;
                returnData.recordsTotal = dataset.result.total;
                returnData.recordsFiltered = dataset.result.total;
                returnData.data = DyyBase.clearNull(dataset.result.list);
                callback(returnData);
            });
        },
        'drawCallback': function () {
            var th = $table_room.find('th');
            var empty = $table_room.find('td.dataTables_empty');
            empty.attr('colspan',th.length);
            if($map_span.hasClass('active')){
                $map_span.removeClass('active');
                DyyBase.showMap('map', $table_room, 'JQ_dyMap', [{name:'gisId',type:'valveRoom'}]);
            }
        },
        'columns': [
            {
                data: 'name', render: function (data, type, row) {
                return '<span title="' + data + '">' + data + '</span><input type="hidden" value="' + row.gisId + '" name="gisId">';
            }
            },
            {
                data: 'customNumber', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'type', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'locationDescription', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'isPiggingDeviceind', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'isPowerind', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'isEmptyingDevice', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'id', render: function (data) {
                return '<i title="详情" class="zy-details-icon" data-id="' + data + '"></i>';
            }
            }
        ]
    });

    // 搜索
    $('#JQ_search_btn').click(function () {
        if(DTROOM){
            DTROOM.draw();
        }
    });

    // 显示地图
    $map_span.click(function () {
        $export_base.hide();
        DyyBase.showMap('map', $table_room, 'JQ_dyMap', [{name:'gisId',type:'valveRoom'}]);
    });

    // 显示列表
    $list_span.click(function () {
        $export_base.show();
        DyyBase.showMap('list', $table_room, 'JQ_dyMap', [{name:'gisId',type:'valveRoom'}]);
    });

    // 显示详情
    $table_room.on('click', 'i.zy-details-icon', function () {
        DyyBase.showDetails($(this), DTROOM, $table_room, pipeTemplate['room']);
    });

    // 导出
    $export_base.click(function () {
        var param = (filter.getExport instanceof Function) ? filter.getExport() : filter.getExport;
        $(this).EXPT({
            type: 'name',
            name: '场站阀室信息表',
            param: param,
            server: baseDataApi.url_valve_room_export()
        });
    });

})(jQuery, window, document);